<?php
ini_set('display_errors','On');
// $Id: index.php,v 1.94 2007/12/26 08:46:48 dries Exp $

/**
 * @file
 * The PHP page that serves all page requests on a Drupal installation.
 *
 * The routines here dispatch control to the appropriate handler, which then
 * prints the appropriate page.
 *
 * All Drupal code is released under the GNU General Public License.
 * See COPYRIGHT.txt and LICENSE.txt.
 */


//getsams();
//$tempp[name] = $_SESSION['oucu'];

//$_POST[pass] = "password";
//$_POST[op] "Log in";

require_once './includes/bootstrap.inc';

drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
getsams();
//echo $_SESSION['oucu'];
//$_SESSION['oucu']='jdg2';
//$_SESSION['isstudent']= true;
if ($_SESSION['oucu'])
{
	$puser =((grabuserdetails()));
	//var_dump($puser);
	$xuser=( db_fetch_object(db_query("SELECT u.* FROM {users} u  WHERE name = '{$_SESSION['oucu']}'", $key)));
	// if no user, make one
	if (!$xuser){
		user_save("",$puser);
		$xuser=( db_fetch_object(db_query("SELECT u.* FROM {users} u  WHERE name = '{$_SESSION['oucu']}'", $key)));
		// update to set if student
		if ($_SESSION['isstudent']=='true')
		{
			db_query(" insert into users_roles select ". $xuser->uid." as uid, rid from role where name ='student'",$key);
		} // else we are staff - to add if AL
		else
		{
			db_query(" insert into users_roles select ". $xuser->uid." as uid, rid from role where name ='admin'",$key);
			db_query(" insert into users_roles select ". $xuser->uid." as uid, rid from role where name ='staff'",$key);
			db_query("insert into users_roles select ". $xuser->uid." as uid, rid from role where name in (select v_locn_one_desc from all_oucu.staff_dir where v_oucu ='{$xuser->name}');",$key);
		}
		// redirect to load the new user
		header("location: /");
	}
	//load the user from sams cookie
	$user->uid = $xuser->uid;
	$user->name = $xuser->name;
	$user->init =$_SESSION['name'];
	if ($_GET['q']=='node')
	{
		
		header("location: /");
		die("here");
	}
	
//echo 	 
}
$return = menu_execute_active_handler();

// Menu status constants are integers; page content is a string.
if (is_int($return)) {
	switch ($return) {
		case MENU_NOT_FOUND:
			drupal_not_found();
			break;
		case MENU_ACCESS_DENIED:
			drupal_access_denied();
			break;
		case MENU_SITE_OFFLINE:
			drupal_site_offline();
			break;
	}
}
elseif (isset($return)) {
	// Print any value (including an empty string) except NULL or undefined:
	print theme('page', $return);
}

drupal_page_footer();


function grabuserdetails()
{
// print_r($_SESSION);
	$templink = mysql_connect ( 'libraryred1.open.ac.uk', 'xxx', 'password' );
//	if (! $templink) {
	//	die ( 'Could not connect: ' . mysql_error () );
//	}
	mysql_select_db("all_oucu",$templink);
	if ($_SESSION['isstudent']=='true')
	{
		$query ="select * from all_oucu.students where vc_u ='{$_SESSION['oucu']}'";
		//echo $query;
		$roles = array('authenticated user','student');
		$res = mysql_query($query);
		$data = mysql_fetch_array($res);
		$name = $data['vc_u'];
		$email = $data['vc_u'].'@openmail.open.ac.uk';
		//$fname = $data['v_forename']." ".$data['v_surname'];
		$data = array(
                'name' => $name,
                'pass' =>  rand(1, 99999999),
                'status' =>  1,
                'mail' => $email,
                'init' => $fname,
				'roles'=> $roles,
		);
		//print_r($data);
	}
	else
	{
		$query ="select * from all_oucu.staff_dir where v_oucu = '{$_SESSION['oucu']}'";
		//echo $query;
		$res = mysql_query($query);
		$dataa = mysql_fetch_array($res);
		$name = $dataa['v_oucu'];
		$email = $dataa['v_email'].'@open.ac.uk';
		$fname = $dataa['v_forename']." ".$data['v_surname'];
		$data = array(
                'name' => $name,
                'pass' =>  rand(1, 99999999),
                'status' =>  1,
                'mail' => $email,
                'init' => $fname,
		);

	}
// print_r($data);
	return ($data);



	//die("cake1");

}



function getsams()
{
	session_start();
if ($_GET['sams']){
				
			$_SESSION['samstimecheck']=mktime();	
			header("location:".($_GET['url']));
			}
	
	if((mktime() - $_SESSION['samstimecheck'])<= 360 || $_GET['frc']){
		//print_r($_SESSION);
		//echo mktime() - $_SESSION['samstimecheck'];
		//echo "i've checked sams since i made this session";
		//die();
	}
	else{
		if (isset($_COOKIE['SAMSsession'] ))
		{
		
		//	print_r($_SESSION);
		//	print_r($_GET);
			if ($_GET['sams']){
				
			$_SESSION['samstimecheck']=mktime();	
			}
			//die();
		//	header("location:http://libraryred1.open.ac.uk/drupal/sams?url=".$_SERVER['REQUEST_URI']."&".$_SESSION['samstimecheck']);
		//	echo "<hr>".$_SESSION['samstimecheck'];
			//die("hmm,should i be here?");
			
		}
	}
	
	
	$ss = $_COOKIE['SAMSsession'];

	$sid = $_COOKIE['HS7BDF'];

	$name =substr($sid,0,strpos($sid,"\\"));

	$temp = substr($ss,40);

	$temp1=explode(".",$temp);

	$temp1=array_unique($temp1);

	$oucu=$temp1[0];

	$ssid=$temp1[1];

	$isstudent='false';

	if($ssid)$isstudent=true;

	if (!$ssid) $ssid=$temp1[2];
	$_SESSION['oucu']= $oucu;
	$_SESSION['ssid']=$ssid;
	$_SESSION['name']=$name;
	$_SESSION['isstudent'] =$isstudent;
	//$_SESSION['isstudent']=true;
}
